﻿using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Citi.Helpers;

namespace Citi.ResourceConsolidation.WindowUI.BoExtraction
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            var arguments = args.ToList();
            var reportName = MainArgument.GetArgumentValue("reportName", arguments, '-');

            try
            {
                var url = MainArgument.GetArgumentValue("url", arguments, '-');
                var boUrl = MainArgument.GetArgumentValue("boUrl", arguments, '-');
                var userLogin = MainArgument.GetArgumentValue("userLogin", arguments, '-');
                var pathToSave = MainArgument.GetArgumentValue("pathToSave", arguments, '-');
                var parameters = MainArgument.GetArgumentValue("parameters", arguments, '-');
                var newWindowNoAction = MainArgument.GetArgumentValue<bool>("newWindowNoAction", arguments, '-');

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new EOBusinessObjectsExtraction(reportName, url, userLogin, pathToSave, parameters, newWindowNoAction, boUrl));
            }
            catch (Exception e)
            {
                var logPath = string.Format(ConfigurationManager.AppSettings["LogPath"], reportName);
                if (File.Exists(logPath)) File.Delete(logPath);

                var writer = new ConsoleInFile(logPath);
                Console.SetOut(writer);
                Console.WriteLine("#ERROR {0}", e.Message);
            }
        }
    }
}
